package kr.pe.johnkim.checkmileage.mobile.rest.controller;

import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMember;
import kr.pe.johnkim.checkmileage.mobile.util.CheckMileageCommon;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 모바일 서비스 서버외 다른 모듈에게 Push Notification 기능을 제공하는 클래스이다.
 * (ex : 웹사이트에서 이벤트를 등록하면 아래의 메소드를 호출한다.)
 *
 * @author : johnkim <cale9797@gmail.com / Bettersoft, Inc.>
 * @date: 12. 11. 12.
 */

@Controller
@Path("/checkMileageNotificationController")
public class CheckMileageNotificationController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(CheckMileageNotificationController.class);

    @Autowired
    private CheckMileageCommon checkMileageCommon = null;

    /**
     * Android 사용자에게 Notification 을 보낸다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/pushNotificationToAndroid")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMember pushNotificationToAndroid(InputStream is) throws Exception {
        logger.info("pushNotificationToAndroid");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("checkMileageMember");

        CheckMileageMember parameterMember = new CheckMileageMember();
        parameterMember.setRegistrationId(jsonMember2.getString("registrationId"));
        parameterMember.setMessageContent(jsonMember2.getString("messageContent"));

        CheckMileageMember resultMember = new CheckMileageMember();

        try {
            checkMileageCommon.sendASMessage(parameterMember.getRegistrationId(), parameterMember.getMessageContent());
            resultMember.setResult("SUCCESS");
        } catch (Exception e) {
            resultMember.setResult("FAIL");
            logger.error("Fail to push notification to Android.");
        }

        return resultMember;
    }

    /**
     * iOS 사용자에게 Notification 을 보낸다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/pushNotificationToiOS")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMember pushNotificationToiOS(InputStream is) throws Exception {
        logger.info("pushNotificationToiOS");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("checkMileageMember");

        CheckMileageMember parameterMember = new CheckMileageMember();
        parameterMember.setRegistrationId(jsonMember2.getString("registrationId"));
        parameterMember.setMessageContent(jsonMember2.getString("messageContent"));

        CheckMileageMember resultMember = new CheckMileageMember();

        try {
            checkMileageCommon.sendIPMessage(parameterMember.getRegistrationId(), parameterMember.getMessageContent());
            resultMember.setResult("SUCCESS");
        } catch (Exception e) {
            resultMember.setResult("FAIL");
            logger.error("Fail to push notification to iOS.");
        }

        return resultMember;
    }

    /**
     * 테스트용.
     *
     * iOS 사용자에게 Notification 을 보낸다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/testPushNotificationToiOS")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMember testPushNotificationToiOS(InputStream is) throws Exception {
        logger.info("testPushNotificationToiOS");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("checkMileageMember");

        CheckMileageMember parameterMember = new CheckMileageMember();
        parameterMember.setRegistrationId(jsonMember2.getString("registrationId"));
        parameterMember.setMessageContent(jsonMember2.getString("messageContent"));

        CheckMileageMember resultMember = new CheckMileageMember();

        try {
            checkMileageCommon.testSendIPMessage(parameterMember.getRegistrationId(), parameterMember.getMessageContent());
            resultMember.setResult("SUCCESS");
        } catch (Exception e) {
            resultMember.setResult("FAIL");
        }

        return resultMember;
    }
}
